package com.smxy.daydayup.service.serviceImpl;

import com.smxy.daydayup.pojo.ao.QuestionAo;
import com.smxy.daydayup.util.datetime.DateUtil;
import com.smxy.daydayup.util.list.QuestionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.smxy.daydayup.pojo.Question;
import com.smxy.daydayup.mapper.QuestionMapper;
import com.smxy.daydayup.service.QuestionService;

import java.util.List;

/**
 * @team daydayup
 * @time 2021-12-06 21:45
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Resource
    private QuestionMapper questionMapper;
//    @Autowired
//    private Question question;
//    @Autowired
//    private QuestionAo questionAo;


    @Override
    public int deleteByPrimaryKey(Long questionId) {
        return questionMapper.deleteByPrimaryKey(questionId);
    }

    @Override
    public int insert(Question record) {
        return questionMapper.insert(record);
    }

    @Override
    public int insertSelective(Question record) {
        return questionMapper.insertSelective(record);
    }

    @Override
    public QuestionAo selectByPrimaryKey(Long questionId) {
        Question question = new Question();
        QuestionAo questionAo = new QuestionAo();
        question = questionMapper.selectByPrimaryKey(questionId);
        questionAo.setCourseId(question.getCourseId());
        questionAo.setQuestionId(question.getQuestionId());
        questionAo.setQuestionSortId(question.getQuestionSortId());
        questionAo.setQuestionTitle(question.getQuestionTitle());
        questionAo.setQuestionOption(QuestionUtil.resolveString(question.getQuestionOption()));
        questionAo.setQuestionAnswer(QuestionUtil.resolveString(question.getQuestionAnswer()));
        questionAo.setQuestionResolve(question.getQuestionResolve());
        questionAo.setQuestionHardLevel(question.getQuestionHardLevel());
        questionAo.setUserName(question.getUserName());
        questionAo.setIsDelete(question.getIsDelete());
        questionAo.setQuestionSort(question.getQuestionSort());
        return questionAo;
    }

    @Override
    public int updateByPrimaryKeySelective(Question record) {
        return questionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Question record) {
        return questionMapper.updateByPrimaryKey(record);
    }

    @Override
    public int createQuestion(QuestionAo record) {
        Question question = new Question();
//        Question question = new Question(null,record.getQuestionSortId(),record.getQuestionTitle(),
//                QuestionUtil.resolveOption(record.getQuestionOption()),QuestionUtil.resolveAnswer(record.getQuestionAnswer()),
//                record.getQuestionResolve(),record.getQuestionHardLevel(),record.getCourseId(),
//                0,record.getUserName(), DateUtil.getNowDate(),DateUtil.getNowDate());
        question.setQuestionSortId(record.getQuestionSortId());
        question.setQuestionTitle(record.getQuestionTitle());
        question.setQuestionOption(QuestionUtil.resolveList(record.getQuestionOption()));
        question.setQuestionAnswer(QuestionUtil.resolveList(record.getQuestionAnswer()));
        question.setQuestionResolve(record.getQuestionResolve());
        question.setQuestionHardLevel(record.getQuestionHardLevel());
        question.setCourseId(record.getCourseId());
        question.setIsDelete(0); //
        question.setUserName(record.getUserName());
        question.setGmtCreate(DateUtil.getNowDate()); //
        question.setGmtModify(DateUtil.getNowDate()); //

        if (questionMapper.insertSelective(question) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int deleteByLogic(Long questionId, boolean flag) {
        if (questionMapper.deleteByLogic(questionId, flag) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public List<Question> selectQuestionList(Long courseId, Integer isDelete) {
        return questionMapper.selectQuestionList(courseId, isDelete);
    }

    @Override
    public List<Question> selectQuestionListBySort(Long courseId, Long questionSortId, Integer isDelete) {
        return questionMapper.selectQuestionListBySort(courseId, questionSortId, isDelete);
    }

}
